<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="togghide" @mouseenter="show_cate_nav_onfun">
        <h2 class="all">全部商品分类</h2>
        <transition name="cate_nav">
          <div class="sort" v-show="isShow_cate_nav">
            <div class="all-sort-list2" @click="go_search_onfun">
              <div class="item active" v-for="(item, index) in goodsCategoryList" :key="item.categoryId" @mouseenter="togge_show(index)">
                <h3 :data-CategoryName="item.categoryName" :data-CategoryId1="item.categoryId">
                  <a href="javascript:;">{{ item.categoryName }}</a>
                </h3>
                <div class="item-list clearfix" :style="index === currentIndex ? 'display:block;' : ''">
                  <div class="subitem">
                    <dl class="fore" v-for="item_2 in item.categoryChild" :key="item_2.categoryId">
                      <dt>
                        <a :data-CategoryName="item_2.categoryName" :data-CategoryId2="item_2.categoryId" href="javascript:;">{{
                          item_2.categoryName
                        }}</a>
                      </dt>
                      <dd>
                        <em v-for="item_3 in item_2.categoryChild" :key="item_3.categoryId">
                          <a :data-CategoryName="item_3.categoryName" :data-CategoryId3="item_3.categoryId" href="javascript:;">{{
                            item_3.categoryName
                          }}</a>
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
const { throttle } = require('lodash')
export default {
  data() {
    return {
      currentIndex: -1,
      isShow_cate_nav: this.$route.path === '/home' ? true : false,
    }
  },
  methods: {
    togge_show: throttle(function (index) {
      this.currentIndex = index
    }, 600),
    togghide() {
      clearTimeout(this.timer_1)
      if (!(this.$route.path === '/home')) {
        this.isShow_cate_nav = false
      }
      this.currentIndex = -1
    },
    go_search_onfun(e) {
      const { categoryname, categoryid1, categoryid2, categoryid3 } = e.target.dataset
      if (categoryname) {
        //代表点击的是a标签
        let location = {
          name: 'search',
          query: { ...this.$route.query },
          params: { ...this.$route.params },
        }
        location.query.categoryName = categoryname

        location.query.category1Id = undefined
        location.query.category2Id = undefined
        location.query.category3Id = undefined
        if (categoryid1) {
          location.query.category1Id = categoryid1
        } else if (categoryid2) {
          location.query.category2Id = categoryid2
        } else if (categoryid3) {
          location.query.category3Id = categoryid3
        }

        this.$router.push(location)
      }
    },
    show_cate_nav_onfun() {
      this.timer_1 = setTimeout(() => {
        this.isShow_cate_nav = true
      }, 300)
    },
  },
  computed: {
    ...mapState('goodsCategory', ['goodsCategoryList']),
  },
}
</script>

<style lang="less" scoped>
.cate_nav-enter,
.cate_nav-leave-to {
  // transform: rotate(0deg);
  height: 0px;
  opacity: 0;
}
.cate_nav-enter-to,
.cate_nav-leave {
  // transform: rotate(360deg);
  height: 461px;
  opacity: 1;
}
.cate_nav-enter-active,
.cate_nav-leave-active {
  transition: all 0.4s linear;
}

.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;
      // overflow-y: auto;
      // overflow-x: hidden;
      // &::-webkit-scrollbar {
      //   width: 0;
      // }
      .all-sort-list2 {
        height: 100%;
        display: flex;
        flex-direction: column;
        .item {
          flex: 1;
          a {
            text-decoration: none;
          }
          h3 {
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;
            position: relative;
            width: 100%;
            height: 100%;
            box-sizing: border-box;
            & > a {
              display: inline-block;
              color: #333;
              position: absolute;
              line-height: 27px;
              pointer-events: none;
            }
            &:hover {
              a {
                color: red !important;
              }
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;
            a {
              &:hover {
                color: red;
              }
            }
            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
          &:hover {
            h3 {
              background-color: green;
              a {
                color: #fff !important;
              }
            }
          }
        }
      }
    }
  }
}
</style>